//
//  FYNetworkingRequestGenerator.m
//  FYNetworking
//
//  Created by Felix Yin on 2017/5/5.
//  Copyright © 2017年 Felix Yin. All rights reserved.
//

#import "FYNetworkingRequestGenerator.h"
#import "FYNetworkingRequestStting.h"
#import "AFNetworking.h"
#import "FYRequestBaseParameter.h"

@interface  FYNetworkingRequestGenerator()

@property (nonatomic, strong) AFHTTPRequestSerializer *requestSerializer;

@end

@implementation FYNetworkingRequestGenerator

+(instancetype)shareInstance{
    static dispatch_once_t onceToken;
    static FYNetworkingRequestGenerator *generator = nil;
    dispatch_once(&onceToken, ^{
        generator = [[FYNetworkingRequestGenerator alloc] init];
    });
    return generator;
}

- (NSURLRequest *)request:(FYNetworkingRequestStting *)requestSetting{
    NSMutableURLRequest *request = nil;
    if(requestSetting.requestPath.length < 1){
        return nil;
    }
    NSString *requestURLStr = requestSetting.requestPath;
    NSMutableDictionary *requestDict = nil;
    if(requestSetting.parameter.count > 0){
       requestDict = [[NSMutableDictionary alloc] initWithDictionary:requestSetting.parameter];
    }else{
       requestDict = [[NSMutableDictionary alloc] init];
    }
    [requestDict addEntriesFromDictionary:[[FYRequestBaseParameter shareInstance] deviceInfo]];
    
    NSString *reqeustFilePath = nil;
    __block  NSString *name = nil;
    __block NSString *fileName = nil;
    __block NSString *mineType = nil;
    NSError *error = nil;
    if([requestSetting class] == [FYNetworkingFileRequestStting class]){
        reqeustFilePath = ((FYNetworkingFileRequestStting *)requestSetting).filePath;
        name = ((FYNetworkingFileRequestStting *)requestSetting).dataName; //请求参数
        fileName = ((FYNetworkingFileRequestStting *)requestSetting).fileName; //带后缀
        mineType = ((FYNetworkingFileRequestStting *)requestSetting).mineType;
    }
    if(requestSetting.requestType == FYRequestTypeGet){
        request = [self.requestSerializer requestWithMethod:@"GET" URLString:requestURLStr parameters:[requestDict copy] error:&error];
    }else if (requestSetting.requestType == FYRequestTypePost){
        request = [self.requestSerializer requestWithMethod:@"POST" URLString:requestURLStr parameters:[requestDict copy] error:&error];
    }else if (requestSetting.requestType == FYRequestTypeUpload){
        request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:requestURLStr parameters:requestDict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            if(reqeustFilePath.length > 0){
                NSError *er;
                NSURL *fileURL = [NSURL fileURLWithPath:reqeustFilePath];
                name = name.length < 1 ? @"data" : name;
                fileName = fileName.length < 1 ? @"data.zip" : fileName;
                mineType = mineType.length < 1 ? @"application/zip" : mineType;
                [formData appendPartWithFileURL:fileURL name:name fileName:fileName mimeType:mineType error:&er];
            }
        } error:&error];
    }
    
    if(error){
        return nil;
    }
    return request;
}

- (AFHTTPRequestSerializer *)requestSerializer{
    if(_requestSerializer == nil){
        _requestSerializer = [AFHTTPRequestSerializer serializer];
        _requestSerializer.timeoutInterval = 20;
        _requestSerializer.cachePolicy = NSURLRequestUseProtocolCachePolicy;
    }
    return _requestSerializer;
}

@end
